﻿using Caliburn.Micro;
using Domain;
using LearnEnglishWords.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.SpeechSynthesis;
using IBE = Common.IllegalBehaviorException;

namespace LearnEnglishWords.UI.ViewModels
{
    public class WordDemostrationViewModel : BaseViewModel
    {
        private Language _selectedLanguage;
        public Language SelectedLanguage
        {
            get { return _selectedLanguage; }
            set { _selectedLanguage = value; NotifyOfPropertyChange(); }
        }

        private EnglishCardViewModel _englishCard;
        public EnglishCardViewModel EnglishCard
        {
            get { return _englishCard; }
            set { _englishCard = value; NotifyOfPropertyChange(); }
        }

        private RussianCardViewModel _russianCard;        
        public RussianCardViewModel RussianCard
        {
            get { return _russianCard; }
            set { _russianCard = value; NotifyOfPropertyChange(); }
        }

        private Boolean _isKnownHitVisible;
        public Boolean IsKnownHitVisible
        {
            get { return _isKnownHitVisible; }
            set { _isKnownHitVisible = value; NotifyOfPropertyChange(); }
        }


        public WordDemostrationViewModel() 
        {
            SelectedLanguage = Language.ENGLISH;
            EnglishCard = IoC.Get<EnglishCardViewModel>();
            RussianCard = IoC.Get<RussianCardViewModel>();            
        }

        void _circlesModel_WordChanged()
        {
            Init();
        }

        protected override void OnActivate()
        {
            base.OnActivate();

            _circlesModel.WordChanged += _circlesModel_WordChanged;
            Init();
        }

        private void Init()
        {
            IsKnownHitVisible = _circlesModel.IsCurrentCircleKnown();
            SelectedLanguage = Language.ENGLISH;

            EnglishCard.Init();
            RussianCard.Init();
        }

        internal void ChangeLanguage()
        {
            if (SelectedLanguage == Language.ENGLISH)
                SelectedLanguage = Language.RUSSIAN;
            else
                SelectedLanguage = Language.ENGLISH;
        }

        internal void GoToNextWord()
        {
            _circlesModel.NextWord();
        }

        internal async void MarkAsKnowns()
        {
            await _circlesModel.MarkAsKnown();
        }

        internal async Task PlayEnglishWord(Windows.UI.Xaml.Controls.MediaElement Media)
        {
            var word = _circlesModel.CurrentWord;

            if (word == null || String.IsNullOrEmpty(word.EnglishWord))
                return;

            SpeechSynthesisStream stream;
            using (var speechSynthesizer = new SpeechSynthesizer())
            {
                stream = await speechSynthesizer.SynthesizeTextToStreamAsync(word.EnglishWord);
            }

            Media.SetSource(stream, stream.ContentType);
            Media.Play();
        }

        protected override void OnDeactivate(bool close)
        {
            _circlesModel.WordChanged -= _circlesModel_WordChanged;
            base.OnDeactivate(close);
        }
    }

    
}
